package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.Key;

/**
 * 解密工具类
 *
 * @author Liunh
 */
@Slf4j
public class DecryptUtils {

    public static String base64Decode(String input) {
        return new String(Base64.decodeBase64(input));
    }

    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.decodeBase64(key);
    }

    /**
     * 将base64字符串解码为源数据内容
     * 与encode互为相逆的过程
     *
     * @param base64Code base64编码字符串
     * @return byte[] 解码结果。如果参数为null或解码失败，则返回null。
     */
    public static byte[] decodeBase64(String base64Code) {
        if (base64Code == null) {
            return null;
        }
        return Base64.decodeBase64(base64Code);
    }

    /**
     * DES解密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptDES(byte[] data, String key) throws Exception {
        Key k = EncryptUtils.toKey(decryptBASE64(key));

        Cipher cipher = Cipher.getInstance(AlgorithmDefine.ALGORITHM_DES);
        cipher.init(Cipher.DECRYPT_MODE, k);

        return cipher.doFinal(data);
    }

    /*public static String[] decode(String input) {
        try {
            if (input.length() % 2 != 0) {
                return null;
            }
            input = SecurityUtil.reverse(input);
            byte[] bytes = new byte[input.length() / 2];
            for (int i = 0; i <= input.length() - 2; i += 2) {
                String hexString = new StringBuilder().append(input.substring(i + 1, i + 2))
                        .append(input.substring(i, i + 1)).toString();
                if (!(hexString.matches("[0-9a-fA-F]+")))
                    return null;
                bytes[(i / 2)] = (byte) (Integer.parseInt(hexString, 16) - 18);
            }
            String s = new String(bytes);

            s = base64Decode(s);
            String s1 = s.substring(0, "hello".length());
            String s3 = s.substring(s.length() - 13);
            if ((!(s1.equals("hello"))) || (!(s3.matches("\\d+"))))
                return null;
            return new String[]{s.substring("hello".length(), s.length() - 13), s3};
        } catch (Exception e) {
        }
        return null;
    }*/

    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    }
                    outBuffer.append(aChar);
                }
            } else {
                outBuffer.append(aChar);
            }
        }
        return outBuffer.toString();
    }

}